package com.exp.security.config;

import cn.hutool.json.JSONUtil;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.web.session.SessionInformationExpiredEvent;
import org.springframework.security.web.session.SessionInformationExpiredStrategy;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class MySessionInformationExpiredStrategy implements SessionInformationExpiredStrategy {
    @Override
    public void onExpiredSessionDetected(SessionInformationExpiredEvent event) throws IOException {
        // Implement your custom logic here when a session is expired
        HttpServletResponse response = event.getResponse();
        response.setContentType("application/json;charset=UTF-8");

        // 将结果转换成 json 字符串
        Map<String, Object> result = new HashMap();
        result.put("code", 403);
        result.put("msg", "该账号已从其他设备登陆");
        String responseJson = JSONUtil.toJsonStr(result);
        response.getWriter().write(responseJson);
    }
}
